package zhihui.mobile.ui.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zgm.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.message.MessageInfo;

/**
 * Created by 张公铭 on 2017/9/16 0016.
 */

public class MessageAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<MessageInfo> mList = new ArrayList<>();

    public MessageAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<MessageInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    public void addData(List<MessageInfo> list)
    {
        mList.addAll(list);
    }


    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_title, tv_time, tv_desc;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.main_message_item, null);
            tv_title = (TextView) convertView.findViewById(R.id.tv_main_message_item_title);
            tv_time = (TextView) convertView.findViewById(R.id.tv_main_message_item_time);
            tv_desc = (TextView) convertView.findViewById(R.id.tv_main_message_item_desc);

            ViewCache viewCache = new ViewCache();
            viewCache.tv_title = tv_title;
            viewCache.tv_time = tv_time;
            viewCache.tv_desc = tv_desc;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_title = viewCache.tv_title;
            tv_time = viewCache.tv_time;
            tv_desc = viewCache.tv_desc;
        }
        MessageInfo packageMessage = mList.get(position);
        tv_title.setText(packageMessage.getTitle_f());
        tv_time.setText(packageMessage.getCreatetime_f());
        tv_desc.setText(packageMessage.getContent_f());

        return convertView;
    }

    class ViewCache
    {
        public TextView tv_title, tv_time, tv_desc;
    }
}
